package dao

import (
	"context"
	"core/models/db"
	"core/repo"
)

type TypeDao struct {
	repo *repo.Manager
}

func (d *TypeDao) GetValue(ctx context.Context, id uint) string {
	var value string
	d.repo.DB.Client.WithContext(ctx).Model(db.Type{}).Where("id=?", id).Select("name").Scan(&value)
	return value
}

func (d *TypeDao) GetInfo(ctx context.Context, id uint) *db.Type {
	typeModel := new(db.Type)
	d.repo.DB.Client.WithContext(ctx).Model(typeModel).Where("id=?", id).First(typeModel)
	return typeModel
}

func (d *TypeDao) All(ctx context.Context) *[]db.Type {
	typeModel := new([]db.Type)
	d.repo.DB.Client.WithContext(ctx).Model(db.Type{}).Where("is_show=?", 1).Find(typeModel)
	return typeModel
}

func (d *TypeDao) GetTypesIn(ctx context.Context, two, typeTwo int) []int64 {
	typeModel := new(db.Type)
	var typeIn []int64
	whe := make(map[string]any)
	whe["is_sell"] = true
	if two != 0 {
		whe["type"] = two
	}
	if typeTwo != 0 {
		whe["types"] = typeTwo
	}
	d.repo.DB.Client.WithContext(ctx).Model(typeModel).Where(whe).Pluck("id", &typeIn)
	return typeIn
}

func (d *TypeDao) GetCardNext(ctx context.Context, id uint, level int64) *db.TypeCardHaseOneType {
	typeCard := new(db.TypeCardHaseOneType)
	d.repo.DB.Client.WithContext(ctx).Model(db.TypeCard{}).Where("type_id=? AND level=?", id, level).Preload("ConsumeType").Preload("OutputType").First(typeCard)
	return typeCard
}

func (d *TypeDao) GetCardInit(ctx context.Context, id uint) *db.TypeCardHaseOneType {
	typeCard := new(db.TypeCardHaseOneType)
	d.repo.DB.Client.WithContext(ctx).Model(db.TypeCard{}).Where("type_id=? AND is_ini=?", id, true).Preload("ConsumeType").Preload("OutputType").First(typeCard)
	return typeCard
}

func NewTypeDao(m *repo.Manager) *TypeDao {
	return &TypeDao{
		repo: m,
	}
}
